package com.hx.er.common.test;


public class StringUtil {
	public static final String EMPTY = "";

	public static boolean isEmpty(String value) {
		return value == null || "".equals(value.trim());
	}

	public static boolean isCharsLetter(String value) {
		if (value.length() != value.getBytes().length) {
			return false;
		}
		
		for (int i = 0; i < value.length(); i++) {
			if (!Character.isLetter(value.charAt(i))) {
				return false;
			}
		}

		return true;
	}

	public static String firstCharUpperCase(String value) {
		if (value == null) {
			return null;
		} else if (isEqual(EMPTY, value)) {
			return EMPTY;
		} else {
			StringBuilder sb = new StringBuilder(value.toLowerCase());
			sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
			return sb.toString();
		}
	}

	public static boolean isHalf(String value) {
		return value.getBytes().length == value.length();
	}

	public static boolean isNumeric(String value) {
		if (value == null) {
			return false;
		}
		
		try {
			Double.parseDouble(value);
			return true;
		} catch(NumberFormatException e) {
			return false;
		}
	}

	public static boolean checkLength(String value, int minLen, int maxLen) {
		int length = value.getBytes().length;
		return length >= minLen && maxLen >= length;
	}

	public static boolean isEqual(String value1, String value2) {
		if (value1 == null) {
			return value1 == value2;
		} else {
			return value1.equals(value2);
		}
	}

	public static boolean isEmailAddr(String value) {
		return value.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+");
	}

	public static String trim(String value) {
		return value == null ? null : value.trim();
	}

	public static String EmptyToNull(String value) {
		return isEmpty(value) ? null : value;
	}
}
